package net.lab1024.sa.admin.module.exam.service;

import java.util.List;
import net.lab1024.sa.admin.module.exam.dao.QuestionDao;
import net.lab1024.sa.admin.module.exam.domain.entity.QuestionEntity;
import net.lab1024.sa.admin.module.exam.domain.form.QuestionAddForm;
import net.lab1024.sa.admin.module.exam.domain.form.QuestionQueryForm;
import net.lab1024.sa.admin.module.exam.domain.form.QuestionUpdateForm;
import net.lab1024.sa.admin.module.exam.domain.vo.QuestionVO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 试题表 Service
 *
 * @Author cfsjiazy
 * @Date 2025-10-20 16:36:35
 * @Copyright 1.0
 */

@Service
public class QuestionService {

    @Resource
    private QuestionDao questionDao;

    /**
     * 分页查询
     */
    public PageResult<QuestionVO> queryPage(QuestionQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<QuestionVO> list = questionDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(QuestionAddForm addForm) {
        QuestionEntity questionEntity = SmartBeanUtil.copy(addForm, QuestionEntity.class);
        questionDao.insert(questionEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     */
    public ResponseDTO<String> update(QuestionUpdateForm updateForm) {
        QuestionEntity questionEntity = SmartBeanUtil.copy(updateForm, QuestionEntity.class);
        questionDao.updateById(questionEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        questionDao.batchUpdateDeleted(idList, true);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Long id) {
        if (null == id){
            return ResponseDTO.ok();
        }

        questionDao.updateDeleted(id, true);
        return ResponseDTO.ok();
    }
}
